/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.console.web;
import com.credithc.eagleeye.console.service.SettingService;
import com.credithc.eagleeye.console.service.StatService;
import com.credithc.eagleeye.console.service.TraceInfoSearchService;
import com.credithc.eagleeye.console.vo.*;
import com.credithc.eagleeye.vo.RpcTraceInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * <br>
 * 类 名: ConsoleController <br>
 * 描 述: 控制台 <br>
 * 作 者: zj <br>
 * 创 建： 2016年06月21日 <br>
 * 版 本：v1.0.0 <br>
 * <br>
 * 历 史: (版本) 作者 时间 注释
 */
@Controller
@RequestMapping("/console")
public class ConsoleController extends AbstractAction {
	private static final Logger logger = LoggerFactory.getLogger(ConsoleController.class);
	@Autowired
	private TraceInfoSearchService traceInfoSearchService;
	@Autowired
	private StatService statService;
	@Autowired
	private SettingService settingService;

	@Override
	protected String getFlag() {
		return "console_flag";
	}

	@Override
	protected String getTitle() {
		return "鹰眼监控平台";
	}

	@RequestMapping(value = "main", method = RequestMethod.GET)
	public String main(HttpServletRequest request,  ModelMap map) {
		putPublicAttribute(map);
		putPlatformListAttribute(request,map);
		return "console/consoleMain";
	}
	@RequestMapping(value = "getEnums", method = {RequestMethod.GET})
	@ResponseBody
	public ResultVo<EnumVo> getEnums(String platformName,String env,HttpServletRequest request,  ModelMap map) {
		ResultVo<EnumVo> resultVo=new ResultVo();
		try {
			EnumVo enumVo = statService.statEnum(platformName, env);
			PlatformVo platformVo = settingService.loadPlatform(platformName);
			if(platformVo!=null){
				enumVo.setMethodNameList(platformVo.getMethodNameList());
			}
			resultVo.setData(enumVo);
			resultVo.setCode("1");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}

		return resultVo;
	}


	@RequestMapping(value = "queryTraceInfo", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultVo<List<RpcTraceInfoVo>> queryTraceInfo(TraceInfoSearchConditionVo conditionVo, ModelMap map) {
		putPublicAttribute(map);
		ResultVo<List<RpcTraceInfoVo>> resultVo=new ResultVo();
		try {
			TraceInfoSearchRes traceInfoSearchRes = this.traceInfoSearchService.searchTraceInfoList(conditionVo);
			resultVo.setCode("1");
			resultVo.setData(traceInfoSearchRes.getRpcTraceInfoVoList());
			PageVo pageVo = new PageVo();
			pageVo.setTotalCount(traceInfoSearchRes.getTotalCount());
			resultVo.setPage(pageVo);
			resultVo.setMessage("获取TraceInfo成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}

		return resultVo;
	}
	@RequestMapping(value = "queryJavaLog", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultVo<List<JavaLogVo>> queryJavaLog(JavaLogSearchConditionVo conditionVo, ModelMap map) {
		putPublicAttribute(map);
		ResultVo<List<JavaLogVo>> resultVo=new ResultVo();
		try {
			JavaLogSearchRes javaLogSearchRes = this.traceInfoSearchService.searchJavaLogList(conditionVo);
			resultVo.setCode("1");
			resultVo.setData(javaLogSearchRes.getJavaLogVoList());
			resultVo.setMessage("获取JavaLog成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}

		return resultVo;
	}


}
